package com.warm.system.controller.system;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.warm.core.domain.AjaxResult;
import com.warm.core.page.BaseController;
import com.warm.core.page.TableDataInfo;
import com.warm.excel.poi.utils.ExcelUtil;
import com.warm.log.annotation.Log;
import com.warm.log.enums.BusinessType;
import com.warm.auth.helper.LoginHelper;
import com.warm.orm.domain.sys.SysConfig;
import com.warm.system.service.ISysConfigService;
import org.noear.solon.annotation.*;
import org.noear.solon.core.handle.Context;
import org.noear.solon.validation.annotation.Valid;
import org.noear.solon.validation.annotation.Validated;

import java.util.List;

/**
 * 参数配置 信息操作处理
 *
 * @author warm
 */
@Valid
@Controller
@Mapping("/system/config")
public class SysConfigController extends BaseController {
    @Inject
    private ISysConfigService configService;

    /**
     * 获取参数配置列表
     */
    @SaCheckPermission("system:config:list")
    @Get
    @Mapping("/list")
    public TableDataInfo<SysConfig> list(SysConfig config) {
        return configService.selectConfigPage(config);
    }

    @Log(title = "参数管理", businessType = BusinessType.EXPORT)
    @SaCheckPermission("system:config:export")
    @Post
    @Mapping("/export")
    public void export(Context ctx, SysConfig config) {
        List<SysConfig> list = configService.selectConfigList(config);
        ExcelUtil<SysConfig> util = new ExcelUtil<SysConfig>(SysConfig.class);
        util.exportExcel(ctx, list, "参数数据");
    }

    /**
     * 根据参数编号获取详细信息
     */
    @SaCheckPermission("system:config:query")
    @Get
    @Mapping(value = "/{configId}")
    public AjaxResult getInfo(Long configId) {
        return success(configService.selectConfigById(configId));
    }

    /**
     * 根据参数键名查询参数值
     */
    @Get
    @Mapping(value = "/configKey/{configKey}")
    public AjaxResult getConfigKey(String configKey) {
        return success(configService.selectConfigByKey(configKey));
    }

    /**
     * 新增参数配置
     */
    @SaCheckPermission("system:config:add")
    @Log(title = "参数管理", businessType = BusinessType.INSERT)
    @Post
    @Mapping
    public AjaxResult add(@Validated SysConfig config) {
        if (!configService.checkConfigKeyUnique(config)) {
            return error("新增参数'" + config.getConfigName() + "'失败，参数键名已存在");
        }
        config.setCreateBy(LoginHelper.getUsername());
        return toAjax(configService.insertConfig(config));
    }

    /**
     * 修改参数配置
     */
    @SaCheckPermission("system:config:edit")
    @Log(title = "参数管理", businessType = BusinessType.UPDATE)
    @Put
    @Mapping
    public AjaxResult edit(@Validated SysConfig config) {
        if (!configService.checkConfigKeyUnique(config)) {
            return error("修改参数'" + config.getConfigName() + "'失败，参数键名已存在");
        }
        config.setUpdateBy(LoginHelper.getUsername());
        return toAjax(configService.updateConfig(config));
    }

    /**
     * 删除参数配置
     */
    @SaCheckPermission("system:config:remove")
    @Log(title = "参数管理", businessType = BusinessType.DELETE)
    @Delete
    @Mapping("/{configIds}")
    public AjaxResult remove(Long[] configIds) {
        configService.deleteConfigByIds(configIds);
        return success();
    }

    /**
     * 刷新参数缓存
     */
    @SaCheckPermission("system:config:remove")
    @Log(title = "参数管理", businessType = BusinessType.CLEAN)
    @Delete
    @Mapping("/refreshCache")
    public AjaxResult refreshCache() {
        configService.resetConfigCache();
        return success();
    }
}
